﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    public class Attendance
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public Attendance()
        {
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2014-03-11</date> 
        /// <param name="model">model</param>
        /// <returns></returns>
        public decimal Add(Model.Attendance model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_Attendance(");
            strSql.Append("FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FEmplNo,@FAttendanceTime,@FAttendanceIP,@FAttendanceMemo,@FDeptNo)");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FAttendanceTime", SqlDbType.DateTime),
					new SqlParameter("@FAttendanceIP", SqlDbType.NVarChar,20),
					new SqlParameter("@FAttendanceMemo", SqlDbType.NVarChar,-1),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FEmplNo;
            parameters[2].Value = model.FAttendanceTime;
            parameters[3].Value = model.FAttendanceIP;
            parameters[4].Value = model.FAttendanceMemo;
            parameters[5].Value = model.FDeptNo;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Attendance GetModel(string loginID, string today)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAttendanceID,FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo from dbo.t_Attendance ");
            strSql.Append(" where FEmplNo=@FEmplNo and DATEDIFF(DAY,FAttendanceTime,@today )=0");
            strSql.Append(" ORDER BY FAttendanceTime DESC");
            SqlParameter[] parameters = {
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,12),
                    new SqlParameter("@today",SqlDbType.NVarChar,50)};
            parameters[0].Value = loginID;
            parameters[1].Value = today;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Attendance GetModelASC(string loginID, string today)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAttendanceID,FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo from dbo.t_Attendance ");
            strSql.Append(" where FEmplNo=@FEmplNo and DATEDIFF(DAY,FAttendanceTime,@today )=0");
            strSql.Append(" ORDER BY FAttendanceTime ASC");
            SqlParameter[] parameters = {
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,12),
                    new SqlParameter("@today",SqlDbType.NVarChar,50)};
            parameters[0].Value = loginID;
            parameters[1].Value = today;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Attendance DataRowToModel(DataRow row)
        {
            Model.Attendance model = new Model.Attendance();
            if (row != null)
            {
                if (row["FAttendanceID"] != null && row["FAttendanceID"].ToString() != "")
                {
                    model.FAttendanceID = decimal.Parse(row["FAttendanceID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FAttendanceTime"] != null && row["FAttendanceTime"].ToString() != "")
                {
                    model.FAttendanceTime = DateTime.Parse(row["FAttendanceTime"].ToString());
                }
                if (row["FAttendanceIP"] != null)
                {
                    model.FAttendanceIP = row["FAttendanceIP"].ToString();
                }
                if (row["FAttendanceMemo"] != null)
                {
                    model.FAttendanceMemo = row["FAttendanceMemo"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 上午的考勤时间
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="emplNo">员工编号</param>
        /// <returns></returns>
        public DataSet GetAMAttendanceTimeDS(string entityNo, string emplNo, string today)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 FAttendanceID,FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo");
            strSql.Append(" FROM dbo.t_Attendance");
            strSql.Append(" where FEntityNo=@FEntityNo and FEmplNo=@FEmplNo and  DATEDIFF(DAY,FAttendanceTime,@today )=0 ");
            strSql.Append(" ORDER BY FAttendanceTime ASC");
            SqlParameter[] parameters = {
                   new SqlParameter("@FEntityNo",SqlDbType.VarChar,50),
                   new SqlParameter("@FEmplNo",SqlDbType.NVarChar,50),
                   new SqlParameter("@today",SqlDbType.NVarChar,50)};
            parameters[0].Value = entityNo;
            parameters[1].Value = emplNo;
            parameters[2].Value = today;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 下午考勤时间
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="emplNo">员工编号</param>
        /// <returns></returns>
        public DataSet GetPMAttendanceTimeDS(string entityNo, string emplNo, string today)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 FAttendanceID,FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo");
            strSql.Append(" FROM dbo.t_Attendance");
            strSql.Append(" where FEntityNo=@FEntityNo and FEmplNo=@FEmplNo and  DATEDIFF(DAY,FAttendanceTime,@today )=0 ");
            strSql.Append(" ORDER BY FAttendanceTime DESC");
            SqlParameter[] parameters = {
                   new SqlParameter("@FEntityNo",SqlDbType.VarChar,50),
                   new SqlParameter("@FEmplNo",SqlDbType.NVarChar,50),
                   new SqlParameter("@today",SqlDbType.NVarChar,50)};
            parameters[0].Value = entityNo;
            parameters[1].Value = emplNo;
            parameters[2].Value = today;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 所有数据
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="emplNo">员工编号</param>
        /// <returns></returns>
        public DataSet GetAllData(string entityNo, string emplNo, string today)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FAttendanceID,FEntityNo,FEmplNo,FAttendanceTime,FAttendanceIP,FAttendanceMemo,FDeptNo");
            strSql.Append(" FROM dbo.t_Attendance");
            strSql.Append(" where FEntityNo=@FEntityNo and FEmplNo=@FEmplNo and DATEDIFF(DAY,FAttendanceTime,@today )=0 ");
            SqlParameter[] parameters = {
                   new SqlParameter("@FEntityNo",SqlDbType.VarChar,50),
                   new SqlParameter("@FEmplNo",SqlDbType.NVarChar,50),
                   new SqlParameter("@today",SqlDbType.NVarChar,50)};
            parameters[0].Value = entityNo;
            parameters[1].Value = emplNo;
            parameters[2].Value = today;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到下午考勤时间
        /// </summary>
        /// <param name="today"></param>
        /// <returns></returns>
        public DataSet GetWorkTimePMAttendance(string today) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT [FWorkTimeID],[FEntityNo] ,[FBeginDate],[FEndDate],[FAMBeginTime] ,[FAMEndTime],[FPMBeginTime] ,[FPMEndTime],[FAMAttendanceTime],[FPMAttendanceTime] ,[FIsModify]");
            strSql.Append(" FROM t_WorkTime");
            strSql.Append(" WHERE DATEDIFF(DAY,@today1,FBeginDate)<=0  AND DATEDIFF(DAY, @today2, FEndDate)>=0");
            SqlParameter[] parameters = {
                   new SqlParameter("@today1",SqlDbType.NVarChar,50),
                   new SqlParameter("@today2",SqlDbType.NVarChar,50)};
            parameters[0].Value = today;
            parameters[1].Value = today;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

    }
}
